//
//  SMBButton.m
//  Simubang_iphone
//
//  Created by Kelly xu on 16/3/23.
//  Copyright © 2016年 Simubang. All rights reserved.
//

#import "SMBButton.h"
#import "UIControl+UIControl_buttonCon.h"

@implementation SMBButton
@synthesize block = _block;
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
-(id)initWithFrame:(CGRect)frame title:(NSString*)title pressed:(myblock)block{
    if (self == [super initWithFrame:frame]) {
        self.frame = frame;
        [self setTitle:title forState:normal];
        [self setTitleColor:[UIColor blackColor] forState:normal];
        [self addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
        [self.titleLabel setFont:[UIFont systemFontOfSize:16]];
        self.timeInterval = 1;
        self.block = block;
    }
    return self;
}
-(id)initWithFrame:(CGRect)frame title:(NSString*)title pressDown:(beginblock)beginblock out:(endblock)endblock{
    if (self == [super initWithFrame:frame]) {
        self.frame = frame;
        [self setTitle:title forState:normal];
        [self setTitleColor:[UIColor blackColor] forState:normal];
        [self addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
        [self.titleLabel setFont:[UIFont systemFontOfSize:16]];
        self.timeInterval = 1;
        self.beginblock = beginblock;
        self.endblock = endblock;
    }
    return self;
}

-(void)buttonPressed:(UIButton*)button{
    if (self.block) {
       self.block(button);
    }
}

- (instancetype)initWithTitle:(NSString *)title
                    titleFont:(id)titleFont
               titleTextColor:(NSString *)titleTextColor
              backgroundColor:(NSString *)backgroundColor
                         icon:(UIImage *)icon
              backgroundImage:(UIImage *)backgroundImage
                      pressed:(myblock)block
{
    if (self = [super init]) {
        if (title) [self setTitle:title forState:UIControlStateNormal];
        if (titleFont) self.titleLabel.font = titleFont;
        if (titleTextColor) [self setTitleColor:[UIColor colorWithHexString:titleTextColor] forState:normal];
        if (backgroundColor) self.backgroundColor = [UIColor colorWithHexString:backgroundColor];
        if (icon) [self setImage:icon forState:UIControlStateNormal];
        if (backgroundImage) [self setBackgroundImage:backgroundImage forState:UIControlStateNormal];
        [self addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
        self.timeInterval = 1;
        self.block = block;
    }
    return self;
}
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
    BOOL isInside = [super pointInside:point withEvent:event];
    NSLog(@"Button is inside: %zd", isInside);
    return isInside;
}

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    UIView *view = [super hitTest:point withEvent:event];
    NSLog(@"Button hit: %@", view);
    return view;
}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    NSLog(@"Button touches began");
    [super touchesBegan:touches withEvent:event];
    if (self.beginblock) {
        self.beginblock(self);
    }
}

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    NSLog(@"Button touches moved");
    [super touchesMoved:touches withEvent:event];
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    NSLog(@"Button touches ended");
    [super touchesEnded:touches withEvent:event];
    if (self.endblock) {
        self.endblock(self);
    }
}

- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    NSLog(@"Button touches cancelled");
    [super touchesCancelled:touches withEvent:event];
}
@end
